package com.example.bigevent.controller;

import com.example.bigevent.pojo.Category;
import com.example.bigevent.pojo.PageBean;
import com.example.bigevent.pojo.Result;
import com.example.bigevent.service.CategoryService;
import com.example.bigevent.validation.ValidationGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增文章分类
     */
    @PostMapping
    @PreAuthorize("hasAuthority('category:add')")
    public Result add(@RequestBody @Validated Category category){
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 查询该用户所有文章分类
     */
    @GetMapping
    @PreAuthorize("hasAuthority('category:query')")
    public Result<List<Category>> list(){
        List<Category> categoryList=categoryService.list();
        return Result.success(categoryList);
    }

    /**
     * 查询后台文章分类列表
     */
    @GetMapping("/sys/list")
    @PreAuthorize("hasAuthority('system:category:query')")
    public Result<PageBean<Category>> sysList(Integer pageNum,Integer pageSize,Category category){
        PageBean<Category> pageBean=categoryService.sysList(pageNum,pageSize,category);
        return Result.success(pageBean);
    }

    /**
     * 查询文章分类详细信息
     */
    @GetMapping("/detail")
    @PreAuthorize("hasAuthority('category:query')")
    public Result<Category> detail(@RequestParam("id") Integer id){
        Category category=categoryService.findById(id);
        return Result.success(category);
    }

    /**
     * 查询用户所有文章分类
     */
    @GetMapping("/list/userId")
    @PreAuthorize("hasAuthority('system:category:query')")
    public Result<List<Category>> listByUserId(Integer userId){
        List<Category> categoryList=categoryService.listByUserId(userId);
        return Result.success(categoryList);
    }

    /**
     * 更新文章分类，更新后台文章分类
     */
    @PutMapping
    @PreAuthorize("hasAnyAuthority('category:update','system:category:update')")
    public Result update(@RequestBody @Validated(ValidationGroup.UpdateCategory.class) Category category){
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 删除文章分类，删除后台文章分类
     */
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('category:delete','system:category:delete')")
    public Result delete(@RequestParam("id") Integer id){
        categoryService.deleteById(id);
        return Result.success();
    }
}
